<!-- 新增编辑车辆 -->
<template>
  <div>
    <!-- 新增弹框 -->
    <el-dialog
      title="新增车辆"
      :visible.sync="addShowDialog"
      width="30%"
      @close="close"
    >
      <el-form
        ref="addForm"
        :rules="rules"
        :model="carformData"
      >
        <el-form-item
          label="车辆类型"
          prop="truckTypeId"
        >
          <template>
            <el-select
              v-model="carformData.truckTypeId"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in carSelectList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item
          label="车牌号码"
          prop="licensePlate"
        >
          <el-input
            v-model="carformData.licensePlate"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          label="GPS设备ID"
          prop="deviceGpsId"
        >
          <el-input
            v-model="carformData.deviceGpsId"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="mini"
          @click="close"
        >取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="btnAddOk"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getCarTypeList, truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    addShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      carSelectList: [],
      carformData: {
        truckTypeId: '',
        licensePlate: '',
        deviceGpsId: ''

      },
      rules: {
        truckTypeId: [
          { required: true, message: '请输入要添加的车辆名称', trigger: 'blur' }
        ],
        // 车牌号码
        licensePlate: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/, message: '车牌号信息不正确', trigger: 'blur' }

        ],
        // GPS设备id
        deviceGpsId: [
          { required: true, message: '请输入GPS设备Id', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCarTypeList()
  },
  methods: {
    // 获取下拉车辆类型
    async getCarTypeList() {
      const car = await getCarTypeList()
      console.log('下拉车辆类型', car)
      this.carSelectList = car.data
    },

    // 新增取消按钮
    close() {
      this.$refs.addForm.resetFields()
      // this.addShowDialog = false
      this.$emit('update:addShowDialog', false)
    },
    btnAddOk() {
      this.$refs.addForm.validate(async(isOk) => {
        if (isOk) {
          await truckTypeAdd({ ...this.carformData })
          console.log(this.carformData, '444')
          this.close()
          this.$emit('updateCarList')
          this.$message.success('新增车辆成功')
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
